package com.bussiness.system.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: xlr
 * @Date: 2018/8/13 15:10
 * @Description:
 */
@Configuration
public class SpringBeanUtil implements ApplicationContextAware {
	private static ApplicationContext context;
	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		SpringBeanUtil.context = arg0;
	}

    /** 
     * 获取对象 
     * 这里重写了bean方法，起主要作用 
     * @param bean
     * @return Object 一个以所给名字注册的bean的实例 
     * @throws BeansException 
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String bean) throws BeansException {
               return (T) context.getBean(bean);
     }

	public static <T> T getBean(Class bean) throws BeansException {
		return (T) context.getBean(bean);
	}

}
